package com.campus.dao.impl;

import java.util.List;

import javax.annotation.Resource;

import org.hibernate.SessionFactory;
import org.springframework.stereotype.Repository;

import com.campus.dao.UserDao;
import com.campus.entity.User;

@Repository
public class UserDaoImpl implements UserDao {

	@Resource
	private SessionFactory sessionFactory;
	
	@Override
	public List<User> selectList() {
		List<User> list = (List<User>) sessionFactory.getCurrentSession().createQuery("from User").list();
		return list;
	}

	@Override
	public void add(User thisClass) {
		sessionFactory.getCurrentSession().save(thisClass);
	}

	@Override
	public User getById(int id) {
		User myClass = (User) sessionFactory.getCurrentSession().get(User.class, id);
		return myClass;
	}

	@Override
	public void update(User thisClass) {
		sessionFactory.getCurrentSession().update(thisClass);
	}

	@Override
	public void deleteById(int id) {
		User c=new User();
		c.setId(id);
		sessionFactory.getCurrentSession().delete(c);
	}

	@Override
	public List<User> pagingList(int curPage, int pageCount) {
		List<User> list = (List<User>) sessionFactory.getCurrentSession().createQuery("from User").setMaxResults(pageCount)
				.setFirstResult((curPage - 1) * pageCount).list();
		return list;
	}

	@Override
	public int countAll() {
		long n = (Long) sessionFactory.getCurrentSession().createQuery("Select count(u) from User u").uniqueResult();
		return (int)n;
	}



	@Override
	public List<User> checkName(String name) {
		List<User> list = (List<User>) sessionFactory.getCurrentSession().createQuery("from User where name= ?")
				.setParameter(0,name)
				.list();
		return list;
	}

	@Override
	public List<User> checkLikeName(String name) {
		List<User> list = (List<User>) sessionFactory.getCurrentSession().createQuery("from User where name like ?")
				.setParameter(0,"%"+name+"%")
				.list();
		return list;
	}



}
